package com.andy.beef.app.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import androidx.core.text.isDigitsOnly
import com.afollestad.materialdialogs.MaterialDialog
import com.andy.beef.BuildConfig
import com.andy.beef.app.util.LocalDataUtil
import com.andy.beef.databinding.ActivitySplashLayoutBinding
import com.andy.beef.login.activity.LoginActivity
import com.andy.beef.main.activity.MainActivity
import com.andy.module.base.BaseVbActivity
import com.andy.module.bean.AppUpdateBean
import com.andy.module.constant.AppConstant
import com.andy.module.ext.dismissLoadingExt
import com.andy.module.ext.showLoadingExt
import com.blankj.utilcode.util.ToastUtils
import me.hgj.jetpackmvvm.ext.parseState

/**
 * @creator:LuoFan
 * @date:2022/5/25
 * @description:
 */
@SuppressLint("CustomSplashScreen")
class SplashActivity : BaseVbActivity<SplashViewModel, ActivitySplashLayoutBinding>() {

    private var runnable = Runnable {
        toMain(this)
    }

    override fun initView(savedInstanceState: Bundle?) {
        mViewBind.tvVersion.text = "v" + BuildConfig.VERSION_NAME
    }

    override fun onPause() {
        super.onPause()
        mViewBind.tvVersion.removeCallbacks(runnable)
    }

    override fun onResume() {
        super.onResume()
        mViewBind.tvVersion.postDelayed(runnable, 3000)
    }

    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    override fun dismissLoading() {
        dismissLoadingExt()
    }

    override fun createObserver() {
        mViewModel.getVersion()
        mViewModel.updateInfoResult.observe(this) { result ->
            parseState(result, {
                if (!it.downloadUrl.isNullOrEmpty() && !it.versionCode.isNullOrEmpty() && it.versionCode.isDigitsOnly()) {
                    if (it.versionCode.toInt() > AppConstant.VERSION_CODE) {
                        mViewBind.tvVersion.removeCallbacks(runnable)
                        showUpdateDialog(this, it)
                    }
                }
            })
        }
    }

    private fun showUpdateDialog(context: Context, updateBean: AppUpdateBean) {
        val loadingDialog = MaterialDialog(context)
        loadingDialog.title(text = updateBean.updateTitle)
            .message(text = updateBean.updateMessage)
            .positiveButton(text = "确定") {
                downloadApk(updateBean, this)
                ToastUtils.showShort("开始下载")
                toMain(this)
            }
            .negativeButton(text = "取消") {
                toMain(this)
            }
            .cancelable(!updateBean.isForceUpdate)
            .cancelOnTouchOutside(!updateBean.isForceUpdate)
            .cornerRadius(12f)
        loadingDialog.show()
    }

    private fun downloadApk(updateBean: AppUpdateBean, context: Context) {
        val savePath = context.getExternalFilesDir("apk")
        val fileName = "beef_" + updateBean.versionNum + ".apk"
//        DownLoadManager.downloadFile(updateBean.downloadUrl, savePath!!.path, fileName, object : SimpleDownloadListener() {
//            override fun onDownLoadSuccess(key: String, path: String, size: Long) {
//                LogUtils.e("xxxxxxxx", "xxxxxxxxonDownLoadSuccess=" + size)
//            }
//
//            override fun onUpdate(key: String, progress: Int, read: Long, count: Long, done: Boolean) {
//                LogUtils.e("xxxxxxxx", "xxxxxxxxprogress=" + progress)
//            }
//        })
    }

    private fun toMain(context: Context) {
        val token: String = LocalDataUtil.getToken()
        if (!TextUtils.isEmpty(token)) {
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra("id", "100")
            startActivity(intent)
            finish()
        } else {
            val intent = Intent(context, LoginActivity::class.java)
            startActivity(intent)
            finish()
        }
    }
}